﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMN.Entity;
using CMN.Common;
using CMN.Framework;
using CMN.BLL;
using System.Text.RegularExpressions;
using System.Diagnostics;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace CMN.UI
{
    /// <summary>
    /// 文章
    /// </summary>
    public class Normal_ArticleDisplay
    {
        static Normal_ArticleDisplay()
        {
            BaseTemplateDisplay.OnArticlePageDisplay+=new EventHandler<ArticleDisplayArg>(BaseTemplateDisplay_OnArticlePageDisplay);
            
        }
        static Regex htmlRegex = new Regex("<.+?>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        static Regex linkRegex = new Regex("<[ ]*.+?a[ ]*>", RegexOptions.Compiled | RegexOptions.IgnoreCase);

        /// <summary>
        /// 加载公告模板路径
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private static void BaseTemplateDisplay_OnArticlePageDisplay(object o, ArticleDisplayArg arg)
        {
           
            CMN.Config.SiteConfigInfo siteInfo = CMN.Config.SiteConfigs.GetSiteConfig();

            Module_ArticleEntity ariticle = arg.ArticleEntity;
            arg.PageHelper.Title = ariticle.Title;
            
            if (string.IsNullOrEmpty(ariticle.Tags.Trim()))
            {

                arg.PageHelper.Keywords = ariticle.Title;
            }
            else
            {
                arg.PageHelper.Keywords = List(ariticle.Tags);
            }
            arg.PageHelper.EncodeKeywords = Utils.UrlEncode(arg.PageHelper.Keywords);
            arg.PageHelper.Description = ariticle.Summary;
            if (siteInfo.IsZhanQun)
                return;
            if (ariticle != null)
            {

                Common.Filter.FastFilter ff = MvcWebCacheManager.GetFastFilter();
                //Stopwatch timer;
                //timer = new Stopwatch();
                //timer.Start();
                //ariticle.TextContent = ff.Replace(ariticle.TextContent);
                //timer.Stop();
                //AppLog.Write(string.Concat("FastFilter:", "用时", timer.ElapsedMilliseconds.ToString()), AppLog.LogMessageType.Debug);
                #region 过滤 区分html
                Int32 index = 0;
                //Normal_AdvertiseEntity adv = null;
                //adv = MvcWebCacheManager.GetNormal_Advertise("Content_P");
                //if (adv == null)
                //{
                //    adv = new Normal_AdvertiseEntity();
                //}


                MatchCollection coll = linkRegex.Matches(ariticle.TextContent);
                StringBuilder sb = new StringBuilder();
                if (coll.Count > 0)
                {
                    Normal_ChannelEntity channel = CMN.Framework.MvcWebCacheManager.GetNormal_Channel(ariticle.ChannelId);
                    if (channel != null)
                    {
                        string[] strsMatch = new string[coll.Count];



                        for (int i = 0; i < coll.Count; i++)
                        {

                            strsMatch[i] = coll[i].Value;
                        }

                        for (int i = 0; i < 1; i++)
                        {
                            Random random = new Random();
                            index = random.Next(strsMatch.Length);

                            strsMatch[index] = string.Concat(strsMatch[index], "<span id=\"s", index.ToString(), "\" class=\"none\" ><a  id=\"s", index.ToString(), "\"  href=\"", (channel.Url.ToLower().IndexOf("www.") > -1 ? "" : siteInfo.Url), channel.Url, "\">", channel.Title, "</a>", (channel.Url.ToLower().IndexOf("www.") > -1 ? "" : siteInfo.Url), channel.Url, "</span> ");
                            //if (!ariticle.Path.StartsWith("013"))
                            //{
                            //    strsMatch[index] = string.Concat(strsMatch[index], "<p>", adv.Advertise, "</p>");
                            //}

                        }

                        string[] strsSplit = linkRegex.Split(ariticle.TextContent);

                        int length = (strsMatch.Length > strsSplit.Length ? strsMatch.Length : strsSplit.Length);
                        for (int i = 0; i < length; i++)
                        {


                            if (i < strsSplit.Length)
                            {
                                MatchCollection coll2 = htmlRegex.Matches(strsSplit[i]);
                                string[] strsMatchHtml = new string[coll2.Count];
                                string[] strsSplitHtml = htmlRegex.Split(strsSplit[i]);

                                for (int j = 0; j < coll2.Count; j++)
                                {

                                    strsMatchHtml[j] = coll2[j].Value;
                                }
                                int lengthHtml = (strsMatchHtml.Length > strsSplitHtml.Length ? strsMatchHtml.Length : strsSplitHtml.Length);
                                for (int y = 0; y < lengthHtml; y++)
                                {
                                    if (y < strsSplitHtml.Length)
                                    {
                                        sb.Append(ff.Replace(strsSplitHtml[y]));
                                    }

                                    if (y < strsMatchHtml.Length)
                                    {
                                        sb.Append(strsMatchHtml[y]);
                                    }
                                }

                            }

                            if (i < strsMatch.Length)
                            {
                                sb.Append(strsMatch[i]);
                            }


                        }

                        ariticle.TextContent = sb.ToString();
                        strsSplit = null;
                        strsMatch = null;
                        sb.Remove(0, ariticle.TextContent.Length);
                    }
                }
                else
                {
                    MatchCollection coll2 = htmlRegex.Matches(ariticle.TextContent);
                    string[] strsMatchHtml = new string[coll2.Count];
                    string[] strsSplitHtml = htmlRegex.Split(ariticle.TextContent);

                    for (int j = 0; j < coll2.Count; j++)
                    {

                        strsMatchHtml[j] = coll2[j].Value;
                    }
                    int lengthHtml = (strsMatchHtml.Length > strsSplitHtml.Length ? strsMatchHtml.Length : strsSplitHtml.Length);
                    for (int y = 0; y < lengthHtml; y++)
                    {
                        if (y < strsSplitHtml.Length)
                        {
                            sb.Append(ff.Replace(strsSplitHtml[y]));
                        }

                        if (y < strsMatchHtml.Length)
                        {
                            sb.Append(strsMatchHtml[y]);
                        }
                    }
                    ariticle.TextContent = sb.ToString();
                }
                #endregion

            }
        }

       
        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <param name="showNum"></param>
        /// <param name="cacheTime"></param>
        /// <returns></returns>
        public static string List(string strId)
        {
            string strRetrun = string.Empty;
            List<Normal_TagsEntity> noticeList = MvcWebCacheManager.GetNormal_TagsList();

            string[] idList = strId.Split(',');
            for (int i = 0; i < idList.Length; i++)
            {
                foreach (Normal_TagsEntity tag in noticeList)
                {

                    if (tag.Id.ToString() == idList[i])
                    {

                        strRetrun = string.Concat(strRetrun, tag.TagName);

                        break;
                    }

                }
            }
            return strRetrun;
        }

    }

}
